/**
 * User: Obi
 * Date: 13.07.11
 * Time: 22:10
 */
package com.jediflasher.game_client.utils.logging {
	import flash.display.DisplayObject;
	import flash.display.Stage;

	public class Logger {

		private static var _view:DisplayObject;

		private static var history:String = "";

		public static function log(message:String, loggerMessageLevel:int = 1):void {
			if (!_view) {
				history += loggerMessageLevel + message + "\n";
				trace(loggerMessageLevel + ": "+ message);
			}
			else {
				var l:LoggerView = _view as LoggerView;
				switch (loggerMessageLevel) {
					case LoggerMessageLevel.ERROR:
						l.addError(message);
						break;
					case LoggerMessageLevel.WARNING:
						l.addWarning(message);
						break;
					case LoggerMessageLevel.INFO:
						l.addInfo(message);
						break;
				}
			}
		}

		public static function createView(stage:Stage):DisplayObject {
			if (!_view && stage) {
				_view = new LoggerView(stage);
				var historyArr:Array = history.split("\n");
				var len:int = historyArr.length;
				for(var i:int = 0; i < len; i++) {
					var message:String = historyArr[i] as String;
					var level:int = parseInt(message.charAt(0));
					log(message.slice(1), level);
				}

			}
			return _view;
		}

		public static function get view():DisplayObject {
			return _view;
		}
	}
}
